<?php
	require "../application/dataAccess.php";
	$da = new dataAccess();
	
	$to = $_POST['to'];
	$university = $_POST['university'];
	$txtNumDaysAbsent = $_POST['txtNumDaysAbsent'];
	$subject = $_POST['subject'];
	$content = $_POST['content'];
	
	$emailList = "";
	$query = null;
	if ($to == "1")//Online
	{
		$sql = "SELECT DISTINCT email FROM history_login WHERE active = 1";
		$query = mysql_query($sql);		
	}
	else if ($to == "2")//Offline
	{
		$sql = "SELECT u.Email as email FROM users u WHERE u.active_status = '1' AND u.Email NOT IN (SELECT h.email FROM history_login h WHERE h.active = 1)";
		$query = mysql_query($sql);		
	}
	else if ($to == "3")//University
	{
		$sql = "SELECT Email as email FROM users WHERE active_status = '1' AND university_name = '" . $university . "'";
		echo $sql;
		$query = mysql_query($sql);		
	}
	else if ($to == "4")//Absent in number of days
	{
		$time = $txtNumDaysAbsent * 60 * 60 * 24;
		$sql = "SELECT u.Email as email FROM users u WHERE u.active_status = '1' AND u.Email NOT IN (SELECT h.email FROM history_login h GROUP BY h.email HAVING MAX(datetime) > $time)";
		$query = mysql_query($sql);		
	}
	else if ($to == "5")//Active
	{
		$sql = "SELECT Email as email FROM users WHERE active_status = '1'";
		$query = mysql_query($sql);		
	}
	else if ($to == "6")//Inactive
	{
		$sql = "SELECT Email as email FROM users WHERE active_status = '0'";
		$query = mysql_query($sql);		
	}	
	else if ($to == "7")//All of users
	{
		$sql = "SELECT Email as email FROM users";
		$query = mysql_query($sql);		
	}
	
	if ($query != null)
	{
		$toEmail = array();
		$header  = 'MIME-Version: 1.0' . "\r\n";
		$header .= 'Content-type: text/html; charset=utf-8' . "\r\n";			
		$header .= 'From: support@studysage.com.au' . "\r\n";
		$count = 0;
		while($row = mysql_fetch_array($query))
		{	
			//echo $row['email'] . ",";
			mail($row['email'], $subject, $content, $header);
			$count++;
		}
		
		if ($count == 0)
		{
			echo "-1";
		}
	}
	else
	{
		echo "-1";
	}
?>